<template>
  <div class="box">
    <el-form ref="form" :rules="rules" :model="data" label-position="left">
      <el-form-item
        label="实例名"
        prop="databaseId"
        class="custom-form-item"
        label-width="100px"
      >
        <el-select
          @change="HandelChange"
          v-model="data.databaseId"
          placeholder="请选择实例名"
          style="width: 60%"
        >
          <el-option
            v-for="item in datasourceInfoList"
            :key="item.id"
            :label="item.dataSourceName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-form
      ref="forms"
      class="custom-form-item"
      :model="currentDatabase"
      label-position="left"
      label-width="90px"
      style="margin-left: 9px"
    >
      <el-form-item label="主机名">
        <el-input
          disabled
          v-model="currentDatabase.ip"
          placeholder="请填写主机名"
          style="width: 60%"
        ></el-input>
      </el-form-item>
      <el-form-item
        class="custom-form-item"
        label="端口"
        style="letter-spacing: 18px"
      >
        <el-input
          disabled
          v-model="currentDatabase.port"
          placeholder="请填写端口"
          style="width: 60%"
        ></el-input>
      </el-form-item>
      <el-form-item class="custom-form-item" label="用户名">
        <el-input
          disabled
          v-model="currentDatabase.uname"
          placeholder="请填写用户名"
          style="width: 60%"
        ></el-input>
      </el-form-item>
      <el-form-item
        style="letter-spacing: 18px"
        class="custom-form-item"
        label="密码"
      >
        <el-input
          disabled
          v-model="currentDatabase.pwd"
          placeholder="请填写密码"
          style="width: 60%"
          type="password"
        ></el-input>
      </el-form-item>
      <el-form-item class="custom-form-item" label="数据模式">
        <el-input
          disabled
          v-model="currentDatabase.schema"
          placeholder="请填写数据模式"
          style="width: 60%"
        ></el-input>
      </el-form-item>
    </el-form>
    <p class="testBtn">
      <el-button
        :type="btnType"
        v-loading="loading"
        size="medium"
        @click="handelClick"
        >测试数据库连接</el-button
      >
    </p>
  </div>
</template>
<script>
import { listDatasourceInfo } from "@/api/console/datasourceInfo";
import { connectTestDatabase } from "@/api/console/tasks.js";
import Vue from "vue";
export default {
  components: {},
  props: ["data"],
  data() {
    return {
      btnType: "primary",
      loading: false,
      datasourceInfoList: null,
      rules: {
        databaseId: [
          { required: true, message: "请选择数据集", trigger: "change" },
        ],
      },
      currentDatabase: {},
    };
  },
  async created() {
    let { start } = this.$props.data;
    const dictItem = await this.getDicts("database_datatype");
    const example = dictItem.data.filter((f) => f.dictLabel == "实例");
    const { rows } = await listDatasourceInfo({
      type: start,
      dataType: example[0].dictValue,
      startNum: 0,
      pgSize: 999,
    });
    this.datasourceInfoList = rows;
    if (this.$props.data.databaseId) {
      this.HandelChange(this.$props.data.databaseId);
      return;
    }
  },
  methods: {
    HandelChange(event) {
      let that = this;
      let filterDataSource = that.datasourceInfoList.filter(
        (f) => f.id == event
      )[0];
      this.$props.data.dataSourceName = filterDataSource.dataSourceName;
      that.currentDatabase = filterDataSource;
      if (!this.$props.data.owner) {
        Vue.set(this.$props.data, "owner", that.currentDatabase.uname);
      }
    },
    handelClick() {
      if (this.$props.data.databaseId.length < 1) {
        return this.$message.warning("请先选择数据集");
      }
      this.loading = true;
      connectTestDatabase(this.currentDatabase)
        .then((res) => {
          this.loading = false;
          if (res.data.code == 200) {
            this.$message.success(res.data.msg);
            this.btnType = "success";
          } else {
            this.btnType = "danger";
            this.$message.error(res.data.msg);
          }
        })
        .catch((err) => {
          this.loading = false;
          this.btnType = "danger";
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.box {
  margin-top: 40px;
  width: 100%;
  margin: 30px auto;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  padding: 30px 40px;

  .testBtn {
    margin: 0px;
    text-align: right;
  }
}
</style>
